package com.huanliscloud.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @author lihuan
 * @since 2024/06/02 09:53:17
 */
@Configuration
@EnableWebSocketMessageBroker
public class StompConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/stomp")
                .addInterceptors()
                .setHandshakeHandler(new HandlerShake())
                .setAllowedOriginPatterns("*")
                .withSockJS();
    }



    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        ThreadPoolTaskScheduler heartbeatTaskPool = new ThreadPoolTaskScheduler();
        heartbeatTaskPool.setPoolSize(10);
        heartbeatTaskPool.setThreadGroupName("websocket");
        heartbeatTaskPool.setThreadNamePrefix("heartbeat");
        heartbeatTaskPool.initialize();
        registry.enableSimpleBroker("/topic")
                .setTaskScheduler(heartbeatTaskPool)
                .setHeartbeatValue(new long[]{1000, 1000});
        registry.configureBrokerChannel()
                        .interceptors(new AuthChannelInterceptor());
        registry.setApplicationDestinationPrefixes("/app");
    }



}
